<template>
  <el-menu
    :default-active="activeIndex"
    class="nav-menu"
    mode="horizontal"
    router
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <el-menu-item index="/">
      <el-icon><Camera /></el-icon>
      <span>摄影约拍</span>
    </el-menu-item>
    
    <el-menu-item index="/photographer-list">
      <el-icon><User /></el-icon>
      <span>摄影师</span>
    </el-menu-item>
    
    <el-menu-item index="/orders" v-if="userStore.isLoggedIn">
      <el-icon><Document /></el-icon>
      <span>我的订单</span>
    </el-menu-item>
    
    <div class="flex-spacer"></div>
    
    <template v-if="userStore.isLoggedIn">
      <el-menu-item v-if="userStore.user && userStore.user.role === 1" @click="goAdmin">
        <el-icon><Setting /></el-icon>
        <span>进入后台管理</span>
      </el-menu-item>
      <el-menu-item index="/profile">
        <el-icon><Setting /></el-icon>
        <span>个人中心</span>
      </el-menu-item>
      <el-menu-item @click="handleLogout">
        <el-icon><SwitchButton /></el-icon>
        <span>退出登录</span>
      </el-menu-item>
    </template>
    
    <el-menu-item index="/login" v-else>
      <el-icon><UserFilled /></el-icon>
      <span>登录</span>
    </el-menu-item>
  </el-menu>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const activeIndex = computed(() => route.path)

const handleLogout = () => {
  userStore.logout()
  ElMessage.success('退出登录成功')
  router.push('/')
}

const goAdmin = () => {
  router.push('/admin/dashboard')
}

onMounted(() => {
  userStore.initUser()
})
</script>

<style scoped>
.nav-menu {
  border-bottom: none;
}

.flex-spacer {
  flex: 1;
}

.el-menu-item {
  display: flex;
  align-items: center;
  gap: 5px;
}
</style> 